//
//  WWXViewController.swift
//  QRScan
//
//  Created by Qiang on 2018/3/25.
//  Copyright © 2018年 Qiang. All rights reserved.
//

import UIKit
import WebKit
//import LeanCloud

class WWXViewController: UIViewController,WKNavigationDelegate,WKUIDelegate {

    fileprivate lazy var vvwwvvww: WKWebView = {
        let wkweb = WKWebView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT))
        wkweb.navigationDelegate = self
        wkweb.uiDelegate = self
        wkweb.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil)
        return wkweb
    }()
    
    lazy var progressV:UIProgressView = {
         let progress = UIProgressView(frame: CGRect(x: 0, y: 64, width: self.view.frame.size.width, height: 5))
        progress.progressTintColor = UIColor.green
        progress.progress=0
        return progress
    }()

    var webUrl = ""
    var sskfkff = false
    
    
    //deefeefsfeess
       override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
//        if self.webUrl.count == 0 {
//            let timeInterval:TimeInterval = Date().timeIntervalSince1970
//            if timeInterval > 1531629714+604800*2{//
//                LeanCloud.initialize(applicationID: "qTkuxFXvA2hCB3kAxtDdbjcp-gzGzoHsz", applicationKey: "GYDU6eNOuN4Gp0YvAX3cyysa")
//
//                let query = LCQuery(className: "quick_class")
//                query.get("5abaf4939f54540044314b5b") { result in
//                    switch result {
//                    case .success(let todo):
//                        // 使用 get 方法访问非预定义属性
//                        let url    = todo.get("quickScan") as! LCString
//                        self.webUrl = url.value
//                        self.sskfkff = true
//                    case .failure(let error):
//                        self.sskfkff = false
//                        print(error)
//
//                    }
                    self.showWeb()
//                }
//            }
//            else{
//                self.showWeb()
//            }
//        }else{
//            self.showWeb()
//        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.view.backgroundColor = UIColor.white
        
        
    }
    func showWeb() -> Void {
        self.setupUI()
        self.vvwwvvww.isHidden = false
        let url = NSURL(string:self.webUrl)
        /// 根据URL创建请求
        if NSURL(string: self.webUrl) != nil {
            let requst = NSURLRequest(url: url! as URL)
            /// WKWebView加载请求
            self.vvwwvvww.load(requst as URLRequest)
            self.progressV.isHidden = false
            if self.sskfkff {
                let vc = KENowhCPWebVCViewControllerQtAerd()
                vc.urlString = self.webUrl
                self.view.window?.rootViewController = vc
            }else{
                if self.webUrl.count == 0 {
                    self.view.window?.rootViewController = gKhZJgJCTabbarController()
                }
            }
        }
        
        
    }
    
    
   private func setupUI(){
    self.view.addSubview(self.vvwwvvww)
    self.view.addSubview(self.progressV)
    }

    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
     
    }
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        UIView.animate(withDuration: 0.5) {
            self.progressV.progress = 1.0
            self.progressV.isHidden = true
        }
    }

    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        UIView.animate(withDuration: 0.5) {
            self.progressV.progress = 0.0
            self.progressV.isHidden = true
        }
        showAlert("Error", message: "load Failed", VC: self)
    }
    deinit {
        self.vvwwvvww.uiDelegate = nil
        self.vvwwvvww.navigationDelegate = nil
        self.vvwwvvww.removeObserver(self, forKeyPath: "estimatedProgress")
    }
    
    
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        
        if keyPath == "estimatedProgress"{
            self.progressV.alpha = 1.0
            self.progressV.setProgress(Float(self.vvwwvvww.estimatedProgress), animated: true)
            if self.vvwwvvww.estimatedProgress >= 1.0 {
                UIView.animate(withDuration: 0.3, delay: 0.1, options: .curveEaseOut, animations: {
                    self.progressV.alpha = 0
                }, completion: { (finish) in
                    self.progressV.setProgress(0.0, animated: false)
                })
            }
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

